package org.example.innovate.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.innovate.domain.History;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author Administrator
* @description 针对表【history】的数据库操作Mapper
* @createDate 2025-03-17 14:55:40
* @Entity org.example.innovate.domain.History
*/
@Mapper
public interface HistoryMapper extends BaseMapper<History> {
    @Select("SELECT h.* FROM history h INNER JOIN " +
            "(SELECT content, MAX(created_at) as max_created_at FROM history WHERE user_id = #{user_id} GROUP BY content) latest " +
            "ON h.content = latest.content AND h.created_at = latest.max_created_at WHERE h.user_id = #{user_id} ORDER BY h.created_at DESC")
    List<History> selectLatestHistories(@Param("user_id") int userId);

    @Select("SELECT * FROM ("
            + "    SELECT *, ROW_NUMBER() OVER (PARTITION BY content ORDER BY created_at DESC) AS rn "
            + "    FROM history "
            + "    WHERE user_id = #{user_id} "
            + ") sub "
            + "WHERE rn = 1 "
            + "ORDER BY created_at DESC")
    List<History> selectLatestHistory(@Param("user_id") int user_id);
}




